package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.UserInterestsRedPacketDao;
import com.sc.nft.entity.UserInterestsRedPacket;
import com.sc.nft.service.UserInterestsRedPacketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserInterestsRedPacketServiceImpl implements UserInterestsRedPacketService {

    private final UserInterestsRedPacketDao userInterestsRedPacketDao;

    /**
     * 批量入库
     *
     * @param userInterestsRedPackets
     * @return
     */
    @Override
    public Integer saveUserInterestsRedPacketBatch(List<UserInterestsRedPacket> userInterestsRedPackets) {
        return userInterestsRedPacketDao.saveUserInterestsRedPacketBatch(userInterestsRedPackets);
    }

    /**
     * 获取当天以及当天之前没有给钱的用户数据
     *
     * @return
     */
    @Override
    public List<UserInterestsRedPacket> selectByFlipFlopTimeScope() {
        return userInterestsRedPacketDao.selectByFlipFlopTimeScope();
    }

    @Override
    public Boolean updateStatusById(Long id) {
        return userInterestsRedPacketDao.updateStatusById(id);
    }

    @Override
    public void addUserRedPacket(Long userId, Date time, BigDecimal price) {
        List<UserInterestsRedPacket> userInterestsRedPackets = new ArrayList<>();
        Integer day = 0;
        for (int i = 0; i < 10; i++) {
            day += 30;
            DateTime dateTime = DateUtil.offsetDay(time, day);
            UserInterestsRedPacket userInterestsRedPacket = new UserInterestsRedPacket();
            userInterestsRedPacket.setUserId(userId);
            userInterestsRedPacket.setAmount(price);
            userInterestsRedPacket.setOrderNo(0L);
            userInterestsRedPacket.setFlipFlopTime(dateTime);
            userInterestsRedPacket.setCreateTime(new DateTime());
            userInterestsRedPackets.add(userInterestsRedPacket);
        }
        // 批量写入到数据库里面
        userInterestsRedPacketDao.saveUserInterestsRedPacketBatch(userInterestsRedPackets);
    }

}
